package net.maku.propagate.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.propagate.convert.PropagateWorkerConvert;
import net.maku.propagate.entity.PropagateWorkerEntity;
import net.maku.propagate.service.PropagateWorkerService;
import net.maku.propagate.query.PropagateWorkerQuery;
import net.maku.propagate.vo.PropagateWorkerVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 劳模风采
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-16
*/
@RestController
@RequestMapping("propagate/worker")
@Tag(name="劳模风采")
@AllArgsConstructor
public class PropagateWorkerController {
    private final PropagateWorkerService propagateWorkerService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('propagate:worker:page')")
    public Result<PageResult<PropagateWorkerVO>> page(@ParameterObject @Valid PropagateWorkerQuery query){
        PageResult<PropagateWorkerVO> page = propagateWorkerService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('propagate:worker:info')")
    public Result<PropagateWorkerVO> get(@PathVariable("id") Long id){
        PropagateWorkerEntity entity = propagateWorkerService.getById(id);

        return Result.ok(PropagateWorkerConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('propagate:worker:save')")
    public Result<String> save(@RequestBody PropagateWorkerVO vo){
        propagateWorkerService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('propagate:worker:update')")
    public Result<String> update(@RequestBody @Valid PropagateWorkerVO vo){
        propagateWorkerService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('propagate:worker:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        propagateWorkerService.delete(idList);

        return Result.ok();
    }
}